#include "MainMenuScene.h"
#include "GameSprite.h"
#include "SelectLevelScene.h"
#include "LevelManager.h"

MainMenuLayer::MainMenuLayer()
{

}

MainMenuLayer::~MainMenuLayer()
{
}

bool MainMenuLayer::init()
{
	bool result = false;

	do
	{
		CC_BREAK_IF(! CCLayerColor::initWithColor(ccc4(255, 255, 255, 255)));
		CCSize size = CCDirector::sharedDirector()->getWinSize();

		// add background
		GameSprite* background = GameSprite::gameSpriteWithFile("background.jpg");
		CC_BREAK_IF(!background);
		background->setPosition(ccp(size.width / 2, size.height / 2));
		this->addChild(background);

		// add logo
		GameSprite* logo = GameSprite::gameSpriteWithFile("Logo.png");
		CC_BREAK_IF(!logo);
		logo->setScale(.8f);
		logo->setPosition(ccp(size.width / 2, size.height * 7 / 8));
		this->addChild(logo);

		CCMenuItemImage *pPlayButton = CCMenuItemImage::create("PlayButton.png", "PlayButton.png", this, menu_selector(MainMenuLayer::playButtonPushCallback));
		CC_BREAK_IF(!pPlayButton);
		pPlayButton->setPosition(ccp(size.width / 2, logo->getPositionY() - 150));

		CCMenu *pMenu = CCMenu::create(pPlayButton, NULL);
		CC_BREAK_IF(!pMenu);
		pMenu->setPosition(CCPointZero);
		this->addChild(pMenu, 1);

		GameSprite* rateUsSprite = GameSprite::gameSpriteWithFile("RateUs.png");
		CC_BREAK_IF(!rateUsSprite);
		rateUsSprite->setPosition(ccp(size.width / 2, logo->getPositionY() - 300));
		this->addChild(rateUsSprite);

		GameSprite* gameCenterSprite = GameSprite::gameSpriteWithFile("GameCenter.png");
		CC_BREAK_IF(!gameCenterSprite);
		gameCenterSprite->setPosition(ccp(size.width / 2, logo->getPositionY() - 450));
		this->addChild(gameCenterSprite);
		result = true;
	}
	while(0);

	return result;
}

void MainMenuLayer::playButtonPushCallback(CCObject* pSender)
{
	SelectLevelScene *pScene = SelectLevelScene::create();
	CCDirector::sharedDirector()->replaceScene(pScene);
}

MainMenuScene::~MainMenuScene()
{
	CC_SAFE_RELEASE_NULL(_layer);
}

bool MainMenuScene::init()
{
	bool result = false;
	do
	{
		CC_BREAK_IF(!CCScene::init());
		_layer = MainMenuLayer::create();
		CC_BREAK_IF(!_layer);
		_layer->retain();
		this->addChild(_layer);
		result = true;
	}
	while(0);
	return result;
}

void MainMenuScene::gameLoop(float time)
{

}